/**
 * DateConverter.java
 * Product:BaseLayer
 * Version:1.0
 * Copyright 2009 by DNE
 * All Rights Reserved.
 */
package com.site.util.base.converter;

import java.util.Map;

import org.apache.struts2.util.StrutsTypeConverter;

/**
 * Struts2类型转换器 重写字符串类型转换器，自动trim
 */
public class StringTypeConverter extends StrutsTypeConverter {
	@Override
	public Object convertFromString(Map context, String[] values, Class toClass) {
		if (values == null || values.length == 0) {
			return null;
		}

		String result = values[0];
		if (result == null) {
			return null;
		}

		result = result.trim();
		if (result.isEmpty()) {
			return null;
		}
		return result;

	}

	@Override
	public String convertToString(Map context, Object object) {
		if (object != null && object instanceof String[]) {
			return ((String[])object)[0].trim().toString();
		}
		else if (object != null && object instanceof String) {
			return object.toString();
		}
		return null;

	}

}
